import KeyCode from '@/KeyCode';
import CoolDown from '@/CoolDown';
import Cache from '@/Cache';
import SelectUtil from "@/SelectUtil";
import UnitUtil from "@/UnitUtil";
import InputUtil from "@/InputUtil";
import BaseUtil from "@/BaseUtil";
import EffectUtil from "@/EffectUtil";
import PlayerUtil from "@/PlayerUtil";

export default class 回城F2Hg {
    static coolDownCache = new Cache();
    /** 传送前摇 */
    static time: number = 0;
    static loc: Vector = null
    static hgCd = 5;


    constructor() {

        InputUtil.onKeyReleased(KeyCode.VK_F2, () => {
            let triggerKeyPlayer = DzGetTriggerKeyPlayer();
            回城F2Hg.hg(triggerKeyPlayer)
        }, true)

        se.onPlayerChat("hg", (e, chatString) => {
            回城F2Hg.hg(e.triggerPlayer)
        })


    }


    static getHgLoc(p: player) {
        return 回城F2Hg.loc || PlayerUtil.getStartLoc(p);
    }


    static hg(p: player) {
        let hero = SelectUtil.getAnHero(GetPlayerId(p));
        if (IsHandle(hero)) {
            let cd = 回城F2Hg.coolDownCache.get(GetPlayerId(p) + "", () => new CoolDown(回城F2Hg.hgCd));
            if (cd.isReady()) {
                if (回城F2Hg.time <= 0) {
                    let loc = 回城F2Hg.getHgLoc(p);
                    UnitUtil.transfer(hero, loc.x, loc.y);
                } else {
                    let effect = EffectUtil.addSpecialEffectTarget("Abilities\\Spells\\Human\\MassTeleport\\MassTeleportTo.mdl", hero)
                    let startX = GetUnitX(hero);
                    let startY = GetUnitY(hero);
                    BaseUtil.onTimer(0.1, (count) => {
                        if (GetUnitX(hero) != startX || GetUnitY(hero) != startY) {
                            DestroyEffect(effect);
                            return false
                        }
                        if (count >= (回城F2Hg.time * 10)) {
                            let loc = 回城F2Hg.getHgLoc(p);
                            UnitUtil.transfer(hero, loc.x, loc.y);
                            //
                            DestroyEffect(effect)
                        }
                        return true;
                    });

                }

            } else {
                PlayerUtil.message("回城冷却中...剩余：" + Math.ceil(cd.getCooldown()) + "秒", 5, GetOwningPlayer(hero))
            }
        }
    }

}